<?php

/**
 * AgencyAdmin Component
 *
 * Handles differences between the site's front-end and admin area in a way that
 * they aren't overridden by app developers (like using 
 * AppController::beforeFilter() would allow) and is easy to include in app
 * controllers.
 *
 * @package       AgencyCMS
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 */
/**
 * AgencyAdminComponent
 */
class AgencyAdminComponent extends Object {

    /**
     * Initialize the component. This is called before the controller's
     * beforeFilter
     *
     * @param Controller $controller
     * @param array $settings
     */
    function initialize(&$controller, $settings = array())
    {

        // Auth component is required
        if (!isset($controller->Auth)) {
            trigger_error('Auth component is required on all controllers');
        }

        if (isset($controller->params['admin']) && $controller->params['admin']) {

            // admin area - switch to admin theme and set up authentication

            // use the admin theme
            $controller->theme = 'admin';

            // set up authentication
            $controller->Auth->userModel = 'AgencyUser';
            $controller->Auth->userScope = array(
                'AgencyUser.can_log_in = 1'
            );
            $controller->Auth->loginAction = array(
                'controller' => 'agency_users',
                'action'     => 'login',
                'admin'      => true
            );

            // auth messages
            $controller->Auth->authError
                = __('You must log in to access that location', true);
            $controller->Auth->loginError
                = __('Invalid username and/or password', true);

            // extra helpers
            $controller->helpers[] = 'time';

            $controller->set('currentUser', $controller->Auth->user());

        } else {

            // front-end page

            // allow unrestricted access to all actions
            $controller->Auth->allow('*');   

        }

        // set the current URL for use in views
        $controller->set(
            'currentUrl',
            substr($controller->here, strlen($controller->base))
        );

    }

}